//
//  BHCircleChoseImageOrVideoViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/6.
//

import UIKit
protocol BHCircleChoseImageOrVideoViewControllerProtocol {
    func didChoseImageActHandler(images: [BHImageOrVideoModel])
    func didChoseVideoActHandler(videoModel: BHImageOrVideoModel?)
}

class BHCircleChoseImageOrVideoViewController: BHBaseViewController {

    var delegate: BHCircleChoseImageOrVideoViewControllerProtocol?
    
    lazy var choseImageOrVideoPicker: BHImageVideoPicker = {
        let view = BHImageVideoPicker.init(frame: CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.height-UIScreen.begainY-UIScreen.safeBottomHeight-64))
        view.loadType = 0
        return view
    }()
    
    lazy var navigationBarView: BHCircleChoseImageOrVideoNavView = {
        let view = BHCircleChoseImageOrVideoNavView.loadFromXib()
        view.delegate = self
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.begainY)
        return view
    }()
    
    lazy var nextBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: 16, y: UIScreen.height-UIScreen.safeBottomHeight-10-44, width: UIScreen.width-32, height: 44)
        btn.backgroundColor = .color_ADE6D2
        btn.setTitle("下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
        btn.addTarget(self, action: #selector(nextPublishAct), for: .touchUpInside)
        btn.addLayerCorner(22, 0, .white)
        return btn
    }()
    
    /**选择类型  0默认tap切换图片和视频  1只能选图片*/
    var choseType: Int = 0
    var entryType: Int = 0  // 1从发布页面进入
    
    
    var addArray: [BHImageOrVideoModel] = [] {
        didSet {
            self.choseImageOrVideoPicker.addArray = addArray
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.choseImageOrVideoPicker.delegate = self
        self.view.addSubview(self.choseImageOrVideoPicker)
        if self.choseType == 0 {
            self.bh_navigationBar.removeFromSuperview()
            self.view.addSubview(self.navigationBarView)
        }else {
            self.bh_navigationBar.title_label.text = "相册"
        }
     
        if self.entryType == 1 {
            self.nextBtn.setTitle("完成", for: .normal)
        }
        
        self.view.addSubview(self.nextBtn)
    }
    
    @objc func nextPublishAct() {
        if self.nextBtn.isSelected == false {
            return
        }
        self.choseImageOrVideoPicker.choseCompleteCall()
    }

}

extension BHCircleChoseImageOrVideoViewController: BHImageVideoPickerDelegate {
    func responseImageChose(imageArray: [BHImageOrVideoModel]) {
        self.delegate?.didChoseImageActHandler(images: imageArray)
        if self.entryType == 1 {
            self.navigationController?.popViewController(animated: true)
        }else {
            let nextPublishVC = BHCirclePublishViewController.init()
            nextPublishVC.addImageArray = imageArray
            self.navigationController?.pushViewController(nextPublishVC, animated: true)
        }
    }
    
    func didCameraTakeImage(image: BHImageOrVideoModel?) {
        if image != nil {
            self.addArray.append(image!)
            
            self.delegate?.didChoseImageActHandler(images: self.addArray)
            if self.entryType == 1 {
                self.navigationController?.popViewController(animated: true)
            }else {
                let nextPublishVC = BHCirclePublishViewController.init()
                nextPublishVC.addImageArray = [image!]
                self.navigationController?.pushViewController(nextPublishVC, animated: true)
            }
        }
    }
    
    func didChoseVideo(video: BHImageOrVideoModel?) {
        if video != nil {
            self.delegate?.didChoseVideoActHandler(videoModel: video)
            
            if self.entryType == 1 {
                self.navigationController?.popViewController(animated: true)
            }else {
                let nextPublishVC = BHCirclePublishViewController.init()
                nextPublishVC.videoModel = video
                self.navigationController?.pushViewController(nextPublishVC, animated: true)
            }
        }
    }
    
    func didChoseArrayNotNull(count: Int) {
        if count == 0 {
            self.nextBtn.backgroundColor = .color_ADE6D2
            self.nextBtn.isSelected = false
        }else {
            self.nextBtn.backgroundColor = .color_34CE98
            self.nextBtn.isSelected = true
        }
    }
}

extension BHCircleChoseImageOrVideoViewController: BHCircleChoseImageOrVideoNavViewDelegate {
    func didNavigationBarSelectIndex(index: Int) {
        if index == 0 {
            self.navigationController?.popViewController(animated: true)
        }
        if index == 1 {
            self.choseImageOrVideoPicker.allowTakeImage = true
            self.choseImageOrVideoPicker.allowMutableChose = true
            self.choseImageOrVideoPicker.loadType = 0
        }
        if index == 2 {
            self.choseImageOrVideoPicker.allowTakeImage = false
            self.choseImageOrVideoPicker.loadType = 1
        }
    }
}
